import sys
import os
import pygame

from ball import Ball
from setting import Settings
from game_status import GameStats



def check_events(case, stats, play_button, ai_settings, exit_button, replay_button):
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            update_best_score(stats)
            sys.exit()

        elif event.type == pygame.KEYDOWN:
            check_keydown_events(event, case)

        elif event.type == pygame.KEYUP:
            check_keyup_events(event, case)

        elif event.type == pygame.MOUSEBUTTONDOWN:
            mouse_x, mouse_y = pygame.mouse.get_pos()
            check_play_button(stats, play_button, mouse_x, mouse_y, ai_settings)
            check_exit_button(stats, exit_button, mouse_x, mouse_y, ai_settings)
            check_replay_button(stats, replay_button, mouse_x, mouse_y, ai_settings)


def check_play_button(stats, play_button, mouse_x, mouse_y, ai_settings):
    button_clicked = play_button.play_rect.collidepoint(mouse_x, mouse_y)

    if button_clicked and not stats.game_active and not stats.game_finish:
        # stats.reset_status()
        stats.game_active = True
        pygame.mouse.set_visible(False)


def check_exit_button(stats, exit_button, mouse_x, mouse_y, ai_settings):
    button_clicked = exit_button.exit_rect.collidepoint(mouse_x, mouse_y)

    if button_clicked and stats.game_finish and not stats.game_active:
        sys.exit()


def check_replay_button(stats, replay_button, mouse_x, mouse_y, ai_settings):
    button_clicked = replay_button.replay_rect.collidepoint(mouse_x, mouse_y)

    if button_clicked and not stats.game_active and stats.game_finish:
        stats.game_replay = True
        # stats.game_active = True
        # stats.game_finish = False
        # pygame.mouse.set_visible(False)


def check_keydown_events(event, case):

    if event.key == pygame.K_RIGHT:
        case.moving_right = True
    elif event.key == pygame.K_LEFT:
        case.moving_left = True


def check_keyup_events(event, case):
    if event.key == pygame.K_RIGHT:
        case.moving_right = False
    elif event.key == pygame.K_LEFT:
        case.moving_left = False


def is_catch(ball, case):
    if case.rect.left <= ball.rect.x <= case.rect.right and ball.rect.y == case.rect.top:
        return True
    else:
        return False


def is_exit(count, max_chance, stats):
    if count >= max_chance:
        update_best_score(stats)
        pygame.mouse.set_visible(True)
        stats.game_finish = True
        stats.game_active = False


def check_high_score(stats, sb):
    if stats.score > stats.high_score:
        stats.high_score = stats.score
        sb.prep_high_score()


def update_best_score(stats):
    with open('highest_score.txt') as f1:
        old_score = int(f1.readline().rstrip())

    current_score = int(round(stats.score, -1))

    with open('highest_score.txt', 'w') as f2:
        if current_score >= old_score:
            f2.write(str(current_score))
        else:
            f2.write(str(old_score))


def check_file():
    if not os.path.exists('highest_score.txt'):
        h_s_f = open('highest_score.txt', 'w')
        h_s_f.write("0")
        h_s_f.close()


def get_stats_score():
    ai_settings = Settings()
    stats = GameStats(ai_settings)
    return stats.score


# def create_ball():
#     ai_settings = Settings()
#     screen = pygame.display.set_mode((ai_settings.screen_width, ai_settings.screen_height))
#     ball = Ball(ai_settings, screen)
#     ball.blitme()

def update_screen(ai_settings, screen, case, ball, stats, play_button, sb, exit_button, replay_button):

    screen.fill(ai_settings.bg_color)
    case.blitme()
    ball.blitme()
    sb.show_score()

    if not stats.game_active and not stats.game_finish:
        play_button.draw_play_button()

    if stats.game_finish:
        exit_button.draw_exit_button()
        replay_button.draw_replay_button()

    pygame.display.flip()





